<?php
declare (strict_types = 1);

namespace app\api\model\v1;

use think\Model;

/**
 * @mixin \think\Model
 */
class Project extends AppModel
{



    //项目一级分类
    public function projectType(){
        return $this->hasMany(ProjectType::class,'project_id');
    }

    //项目二级指标
    public function projectSecond(){
        return $this->hasMany(ProjectSecondLevel::class,'project_id');
    }

    //项目详细指标
    public function projectDetail(){
        return $this->hasMany(ProjectDetail::class,'project_id');
    }

    //项目样本
    public function projectSample(){
        return $this->hasMany(ProjectSample::class,'project_id');
    }
    //文件夹
    public function children(){
        return $this->hasMany(Folder::class,'project_id');
    }
    /**
     * @param $disposeData //数据源
     * @param $project_id //新增项目的id
     * @param $create_id //创建人
     * @User: 刘海龙
     * @Date: 2021/11/11
     * @Time: 11:35
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 同步模版项目
     */
    public static function synchronousData($disposeData, $project_id, $create_id)
    {
        if (!empty($disposeData)) {
            foreach ($disposeData as $key => $value) {
                $project_type_id = ProjectType::create([
                    'project_type_name'=>$value['template_type_title'],
                    'year'=>$value['year'],
                    'project_id'=>$project_id,
                    'create_id'=>$create_id
                ])->id;
                $sample = TemplateSample::where(['template_type_id'=>$value['id']])->select()->toArray(); //查找样本点
                if(!empty($sample)){ //存在样本点
                    foreach ($sample as $k=>$v){
                        ProjectSample::create([
                            'title'=>$v['title'],
                            'project_id'=>$project_id,
                            'project_type_id'=>$project_type_id
                        ]);
                    }
                }
                if (isset($value['templateSecondLevel'])) {
                    foreach ($value['templateSecondLevel'] as $child_key => $child_v) {
                        $project_second_level_id = ProjectSecondLevel::create([
                            'project_second_title'=>$child_v['second_level_title'],
                            'year'=>$child_v['year'],
                            'project_original_score'=>$child_v['original_score'],
                            'project_explain'=>$child_v['explain'],
                            'project_id'=>$project_id,
                            'project_type_id'=>$project_type_id
                        ])->id;
                        if (isset($child_v['templateDetail'])) {
                            foreach ($child_v['templateDetail'] as $child_child_key => $child_child_v) {
                                ProjectDetail::create([
                                    'project_detail_title'=>$child_child_v['template_detail_title'],
                                    'year'=>$child_child_v['year'],
                                    'once_deduct_marks'=>$child_child_v['once_deduct_marks'],
                                    'most_deduct_marks'=>$child_child_v['most_deduct_marks'],
                                    'duty_unit_id'=>$child_child_v['duty_unit_id'],
                                    'project_id'=>$project_id,
                                    'project_type_id'=>$project_type_id,
                                    'project_second_level_id'=>$project_second_level_id,
                                    'create_id'=>$create_id
                                ]);
                            }
                        }
                    }
                }
            }
            $result = 200;
        }else {
            $result = 403;
        }
        return $result;
    }
}
